package com.maple.result;

import com.maple.enumeration.ExceptionEnum;
import com.maple.exception.BusinessException;
import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;

    public static <T> Result<T> success(){
         Result<T> result = new Result<>();
        result.code = 1;
        result.msg = "成功";
        return result;
    }

    public static <T> Result<T> success(T data){
         Result<T> result = new Result<>();
        result.code = 1;
        result.msg = "成功";
        result.data = data;
        return result;
    }

    public static <T> Result<T> error(BusinessException e){
        Result<T> result = new Result<>();
        result.code = e.getCode();
        result.msg = e.getMsg();
        return result;
    }

    public static <T> Result<T> error(Integer code, String msg){
        Result<T> result = new Result<>();
        result.code = code;
        result.msg = msg;
        return result;
    }

    public static <T> Result<T> error(ExceptionEnum e){
        Result<T> result = new Result<>();
        result.code = e.getCode();
        result.msg = e.getMsg();
        return result;
    }

}
